import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import axios from 'axios'
import VueAxios from 'vue-axios'
import { getStore } from '@/untils/storage'
import animated from 'animate.css'
import 'vant/lib/dropdown-menu/style/less';

import {
    ElRow,
    ElCol,
    ElMenu,
    ElMenuItemGroup,
    ElSubmenu,
    ElMenuItem,
    ElForm,
    ElFormItem,
    ElInput,
    ElButton,
    ElIcon,
} from 'element-plus'
import {
    DropdownMenu, DropdownItem,
    Icon,
    Swipe, SwipeItem,
    Lazyload
} from 'vant'

const app = createApp(App);
app.use(store).use(router).use(VueAxios, axios).use(animated)
// element-ui
app.use(ElRow);
app.use(ElCol);
app.use(ElMenuItemGroup);
app.use(ElSubmenu);
app.use(ElMenuItem);
app.use(ElMenu);
app.use(ElForm);
app.use(ElFormItem);
app.use(ElInput);
app.use(ElButton);
app.use(ElIcon);
// vant
app.use(DropdownMenu);
app.use(DropdownItem);
app.use(Icon);
app.use(Swipe);
app.use(SwipeItem);
app.use(Lazyload);

app.mount('#app');

router.beforeEach((to, from, next) => {
    if(!getStore('token')) {
        if (to.matched.some(record => record.meta.auth)) {
            next({
                path: '/login',
                query: {
                    redirect: to.fullPath // 把要跳转的地址作为参数传到下一步
                }
            })
        } else {
            next()
        }
    } else {
        store.commit('ISLOGIN', getStore('id'))
        if (to.path === '/login') {
            router.push({
                path: '/'
            })
        }
        next()
    }
})